import java.util.Stack;

/**
 * @author Sebastian
 * @version 2.0
 * @date 2024/5/21 21:42
 */
// 有效的括号
public class Solution20 {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        char[] charArray = s.toCharArray();
        for (char c : charArray) {
            if (c == '(' || c == '{' || c == '[') {
                stack.push(c);
            } else {
                if (!stack.isEmpty() && leftIsChar(c) == stack.peek())
                    stack.pop();
                else
                    return false;
            }
        }
        return stack.isEmpty();
    }

    private char leftIsChar(char c) {
        if (c == ')') return '(';
        if (c == '}') return '{';
        return '[';
    }
}
